<?php

namespace common\components\wechat\components;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * WechatHttpException represents a "Method Not Allowed" HTTP exception with status code 405.
 *
 * @see https://tools.ietf.org/html/rfc7231#section-6.5.5
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class WechatHttpException extends \yii\web\HttpException {

    /**
     * Constructor.
     * @param string $message error message
     * @param int $code error code
     * @param \Exception $previous The previous exception used for the exception chaining.
     */
    public function __construct($message = null, $lastError = null, $code = 0, \Exception $previous = null) {
        if ($lastError) {
            $code = ArrayHelper::getValue($lastError, 'errcode', $code);
            $message = ArrayHelper::getValue($lastError, 'errmsg', $message);
        }
        parent::__construct(421, $message, $code, $previous);
    }

    /**
     * @return string the user-friendly name of this exception
     */
    public function getName() {
        return 'Wechat Exception';
    }

}
